<h1><?= strtotitle(__('events')) ?></h1>
<p>
    <?= $this->Html->link('+'.__('new') . ' ' . __('event'), '/events/create') ?> |
    <?= $this->Html->link(strtotitle(__('guests')), '/guests') ?> |
    <?= $this->Html->link(__('Repertoire'), '/repertoire') ?>
</p>

<table>
	
	<tr>
		<th><?= __('name') ?></th>
		<th><?= __('date') ?></th>
		<th><?= __('type') ?></th>
		<th></th>
	</tr>
	
	<? if(!empty($events)): ?>
		<? foreach($events as $event): ?>
		<tr>
		    <td><?= $this->Html->link($event['Event']['name'], '/events/edit/' . $event['Event']['id']) ?></td>
			<td>
			    <? 
			        $label = date_local($event['Event']["start"]);
			        if ($event['Event']['end']) {
                        $label .= ' - ' . date_local($event['Event']["end"]);
                    }
			    ?>
			    <?= $label ?>
            </td>
			<td><?= __($event['Event']["type"]) ?></td>
			<td>
				<?= $this->Html->link(__('delete'), array('action'=>'delete', $event['Event']['id']), array('onclick'=>"return confirm('".__("Do you want to delete") . ' ' . strtotitle(__('event')) ."?')")) ?>
				| <?= $this->Html->link(__('print'), array('action'=>'stampa', $event['Event']['id']), array(
				    'target' => '_blank'
                )) ?>
				<? if (! empty($event['EventChild'])): ?>
				    | <?= $this->Html->link(__('contents') . "...", 'javascript:void(0)', array(
			            'onclick' => "$('.event-child-".$event['Event']['id']."').toggle()"
				    )) ?>
				<? endif ?>
			</td>
		</tr>
		<? if (! empty($event['EventChild'])): ?>
    		<? foreach ($event['EventChild'] as $eventChild): ?>
    		<tr class="event-child event-child-<?= $eventChild['parent_id'] ?>">
    		    <td><?= $this->Html->link($eventChild['name'], '/events/edit/' . $eventChild['id']) ?></td>
    			<td>
    			    <? 
    			        $label = date_local($eventChild["start"]);
    			        if ($eventChild['end']) {
                            $label .= ' - ' . date_local($eventChild["end"]);
                        }
    			    ?>
    			    <?= $label ?>
                </td>
    			<td><?= __($eventChild["type"]) ?></td>
    			<td>
    				<?= $this->Html->link(__('delete'), array('action'=>'delete', $eventChild['id']), array('onclick'=>"return confirm('".__("Do you want to delete") . ' ' . strtotitle(__('event')) ."?')")) ?>
    				| <?= $this->Html->link(__('print'), array('action'=>'stampa', $eventChild['id']), array(
                        'target' => '_blank'
                    )) ?>
    			</td>
    		</tr>
    		<? endforeach;?>
		<? endif ?>
		<? endforeach; ?>
	<? else: ?>
	<tr><td colspan="5"><?= __('Empty') ?>.</td></tr>
	<? endif ?>
</table>
<?= $this->element('pagination') ?>
